﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace editor.cfg
{
    public class Bean
    {
        public Defines Defines { get; private set; }
        public string Namespace { get; private set; }
        public string SimpleName { get; private set; }
        public string DescName { get; private set; }
        public string FullName { get; private set; }

        public Bean Parent { get; private set; }

        public List<Field> Fields { get; private set; }
        public List<Field> HierarchyFields { get; private set; }

        public string Delimiter { get; private set; }

        public List<Bean> Children { get; private set; }

        public bool IsDynamic { get { return Children.Count > 0; } }

        public Field GetFieldByName(string name)
        {
            return HierarchyFields.FirstOrDefault(f => f.Name == name);
        }

        public Bean(Defines ds, string module, XmlElement ele, Bean parent = null)
        {
            this.Defines = ds;
            Namespace = module;
            SimpleName = ele.GetAttribute("name");
            DescName = ele.GetAttribute("desc");
            if (string.IsNullOrWhiteSpace(DescName))
                DescName = SimpleName;
            FullName = module + "." + SimpleName;
            Parent = parent;

            if (string.IsNullOrEmpty(SimpleName))
                Err("name is missing");

            Fields = new List<Field>();
            HierarchyFields = new List<Field>();
            if (parent != null)
                HierarchyFields.AddRange(parent.HierarchyFields);

            var fieldNames = HierarchyFields.Select(f => f.Name).ToList();
            //var fieldIds = HierarchyFields.Select(f => f.Id).ToList();

            foreach (XmlNode e in ele)
            {
                if (e.Name != "var") continue;

                var f = new Field(this, e as XmlElement);
                if (string.IsNullOrEmpty(f.Name))
                    Err("字段名 不能为空");
                if (fieldNames.Contains(f.Name))
                    Err($"字段 {f.Name} 重复");
                //if (fieldIds.Contains(f.Id))
                //{
                //    Err($"字段:{f.Name}  id:{f.Id} 重复");
                //}
                fieldNames.Add(f.Name);
                //fieldIds.Add(f.Id);
                Fields.Add(f);

            }
            HierarchyFields.AddRange(Fields);

            Delimiter = ele.GetAttribute("delimiter");

            Children = new List<Bean>();

            foreach (XmlNode e in ele)
            {
                if (e.Name != "bean") continue;

                var child = new Bean(Defines, module, e as XmlElement, this);
                Defines.Add(child);

                if (child.IsDynamic)
                {
                    Children.AddRange(child.Children);
                }
                else
                {
                    Children.Add(child);
                }
            }

            Log.Debug("Bean:{0} parent:{1} delimitor:{2} fields:{3}", FullName, parent?.FullName, Delimiter, string.Join(",", Fields));
        }

        public void Resolve()
        {
            Fields.ForEach(f => f.Resolve());
        }

        private void Err(string msg)
        {
            throw new Exception($"bean:{FullName} {msg}");
        }
    }
}
